package com.kichina.parking.upstream;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kichina.parking.exception.extend.JsonParseException;
import com.kichina.parking.service.logic.bean.ChannelInfoRecord;
import com.kichina.parking.service.logic.service.ChannelInfoRecordService;
import com.kichina.parking.utils.JsonToEntityUtil;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName MonthPayUpstreamHandler
 * @date 2022-09-14 20:03
 * @desc 通道信息业务处理层
 */
@Component
@Slf4j
public class Parking11GateUpstreamHandler {

    private final ChannelInfoRecordService channelInfoRecordService;

    public Parking11GateUpstreamHandler(ChannelInfoRecordService channelInfoRecordService) {
        this.channelInfoRecordService = channelInfoRecordService;
    }

    /**
     * @param jsonObject 数据实体
     * @author Surging
     * @Description 车场首次启用或通道变化后主动上传通道信息业务处理
     * 上传方式：相隔一小时检测一次上传。 通道，gateid 为唯一值，如平台有对应值，请更新通道=即可以
     * @Return void
     * @date 20:53 2022-09-15
     **/
    public void recordParkingGateUpstream(JSONObject jsonObject) throws JsonParseException {

        ChannelInfoRecord channelInfoRecord;
        try {
         channelInfoRecord = JsonToEntityUtil.jsonToEntity(jsonObject, ChannelInfoRecord.class);
            if (Objects.nonNull(channelInfoRecord.getOperateType())) {
                if (channelInfoRecord.getOperateType() == 3) {
                    LambdaQueryWrapper<ChannelInfoRecord> eq = new QueryWrapper<ChannelInfoRecord>().lambda()
                            .eq(ChannelInfoRecord::getParkid, channelInfoRecord.getParkid())
                            .eq(ChannelInfoRecord::getGateid, channelInfoRecord.getGateid())
                            .eq(ChannelInfoRecord::getGatename, channelInfoRecord.getGatename());
                    if (Objects.nonNull(channelInfoRecord.getIostate())){
                        eq = new QueryWrapper<ChannelInfoRecord>().lambda()
                                .eq(ChannelInfoRecord::getParkid, channelInfoRecord.getParkid())
                                .eq(ChannelInfoRecord::getGateid, channelInfoRecord.getGateid())
                                .eq(ChannelInfoRecord::getGatename, channelInfoRecord.getGatename())
                        .eq(ChannelInfoRecord::getIostate, channelInfoRecord.getIostate());
                    }
                    if (Objects.nonNull(channelInfoRecord.getIspass())){
                        eq = new QueryWrapper<ChannelInfoRecord>().lambda()
                                .eq(ChannelInfoRecord::getParkid, channelInfoRecord.getParkid())
                                .eq(ChannelInfoRecord::getGateid, channelInfoRecord.getGateid())
                                .eq(ChannelInfoRecord::getGatename, channelInfoRecord.getGatename())
                                .eq(ChannelInfoRecord::getIspass, channelInfoRecord.getIspass());
                    }
                    if (Objects.nonNull(channelInfoRecord.getIostate())&&Objects.nonNull(channelInfoRecord.getIspass())){
                        eq = new QueryWrapper<ChannelInfoRecord>().lambda()
                                .eq(ChannelInfoRecord::getParkid, channelInfoRecord.getParkid())
                                .eq(ChannelInfoRecord::getGateid, channelInfoRecord.getGateid())
                                .eq(ChannelInfoRecord::getGatename, channelInfoRecord.getGatename())
                                .eq(ChannelInfoRecord::getIostate, channelInfoRecord.getIostate())
                                .eq(ChannelInfoRecord::getIspass, channelInfoRecord.getIspass());
                    }
                    channelInfoRecordService.remove(eq);
                }else {
                    LambdaQueryWrapper<ChannelInfoRecord> eq = new QueryWrapper<ChannelInfoRecord>().lambda()
                            .eq(ChannelInfoRecord::getParkid, channelInfoRecord.getParkid())
                            .eq(ChannelInfoRecord::getGateid, channelInfoRecord.getGateid());
                    List<ChannelInfoRecord> channelInfoRecordList = channelInfoRecordService.list(eq);
                    if (CollectionUtils.isNotEmpty(channelInfoRecordList)){
                        //更新
                        channelInfoRecord.setUpdateTime(new Date());
                        channelInfoRecordService.update(channelInfoRecord, eq);
                    }else {
                        //添加
                        channelInfoRecord.setChannelInfoRecordId(UUIDUtil.getUUID());
                        channelInfoRecord.setUpdateTime(new Date());
                        channelInfoRecord.setCreateTime(new Date());
                        channelInfoRecordService.save(channelInfoRecord);
                    }
                }
            }
        } catch (ParseException e) {
            log.error("数据格式不合法，解析失败");
            throw new JsonParseException("数据格式不合法，解析失败");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
